package com.guard.android;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.guard.location.UserData;
import com.guard.net.NetManager;
import com.guard.structures.SystemStatus;

public class GuardManager {
    private NetManager netManager;
    private String cookie = ""; //ecbd8022c3e85c8bef6d058d6ce43707
    private String SMSCode = null;
    private String phoneNumber = null;
    private final static String preferenceName = GuardManager.class.getName();
    UserData userData;

    private static class SingletonHolder {
        private final static GuardManager instance = new GuardManager();
    }

    public static GuardManager instance() {
        return SingletonHolder.instance;
    }

    private GuardManager() {
        userData = GuardAplication.instance().getUserData();
        netManager = getNetManager();
        SMSCode = loadStringFromPrefs(Constants.SMSCode, "");
        setCookie(loadStringFromPrefs(Constants.Cookie, ""));
        phoneNumber = loadStringFromPrefs(Constants.Phone, "");
    }

    private static String loadStringFromPrefs(String tag, String defaultValue) {
        String savedText = getPrefs().getString(tag, defaultValue);
        return savedText;
    }

    private static SharedPreferences getPrefs() {
        return GuardAplication.instance().getApplicationContext().getSharedPreferences(preferenceName, Activity.MODE_PRIVATE);
    }

    public static int loadIntFromPrefs(String tag, int defaultValue) {
        int savedValue = getPrefs().getInt(tag, defaultValue);
        return savedValue;
    }

    public boolean isLogged() {
        return !getCookie().equals("");
    }

    public SystemStatus getSystemStatusForClient() {
        SystemStatus status = getNetManager().getSystemStatusForClient(getCookie());
        return status;
    }

    public NetManager getNetManager() {
        if (null == netManager) {
            netManager = new NetManager();
        }
        return netManager;
    }

    public boolean registerUserPhone(String phone) {
        return netManager.registerUserPhone(phone);
    }

    public boolean cancel() {
        return netManager.cancel(cookie);
    }

    public String registerUserSms(String sms) {
        Map<String, String> data = new HashMap<String, String>();
        data.put(Constants.SMSCode, SMSCode);
        data.put(Constants.Phone, phoneNumber);
        return netManager.registerUserSms(data);
    }

    public void setSMSCode(String smsCode) {
        SMSCode = smsCode;
        saveStringToPrefs(Constants.SMSCode, SMSCode);
    }

    private void saveStringToPrefs(String tag, String value) {
        Editor editor = getPrefs().edit();
        editor.putString(tag, value);
        editor.commit();
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        saveStringToPrefs(Constants.Phone, phoneNumber);
        this.phoneNumber = phoneNumber;
    }

    public String getCookie() {
        return cookie;
    }

    public void setCookie(String cookie) {
        saveStringToPrefs(Constants.Cookie, cookie);
        this.cookie = cookie;
    }

    public String getUserGeoCoder() {
        Map<String, String> data = new HashMap<String, String>();
        // data.put(Constants.Cookie, this.cookie);
        if (userData.getCurrentPosition() != null && userData.getCurrentPosition().getLatitude() != 0.0) {
            data.put(Constants.X, Double.toString(userData.getCurrentPosition().getLatitude()));
            data.put(Constants.Y, Double.toString(userData.getCurrentPosition().getLongitude()));
        } else {
            return null;
        }
        return netManager.getUserGeoCoder(data);
    }

    public int sendSOS() {
        Map<String, String> data = new HashMap<String, String>();
        data.put(Constants.Cookie, this.cookie);
        String addr = userData.getUserAdrress();
        addr = addr.trim();
        addr = addr.replace(" ", "+");
        data.put(Constants.Address, addr);
        if (userData.getCurrentPosition() != null && userData.getCurrentPosition().getLatitude() != 0.0) {
            data.put(Constants.X, Double.toString(userData.getCurrentPosition().getLatitude()));
            data.put(Constants.Y, Double.toString(userData.getCurrentPosition().getLongitude()));
        }
        return netManager.sendSos(data);
    }

    public String checkBalance() {
        Map<String, String> data = new HashMap<String, String>();
        data.put(Constants.Cookie, this.cookie);
        return netManager.checkBalance(data);
    }

}
